package im.starchat.chat.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import im.starchat.chat.R;
import im.starchat.chat.view.CircularImageButton;
import im.starchat.chat.rest.model.MemberInfo;

import java.util.List;

/**
 * Created by admin on 15/11/7.
 */
public class ProfileContributionAdapter extends BaseAdapter {
    private static final String TAG = ProfileContributionAdapter.class.getSimpleName();

    Context context;
    List<MemberInfo> list;

    public ProfileContributionAdapter(Context context, MemberListCallBack memberListCallBack) {
        this.mMemberListCallBack = memberListCallBack;
        this.context = context;
    }

    public ProfileContributionAdapter(Context context, List<MemberInfo> list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        if(list == null){
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d(TAG, "Adapter getView  " + position);
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        convertView = layoutInflater.inflate(R.layout.layout_member_item, null);
        CircularImageButton headImage = (CircularImageButton) convertView.findViewById(R.id.head_image);
        headImage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        final MemberInfo item = list.get(position);
//        headImage.setImageBitmap(item.getHeadImage());
        if (!TextUtils.isEmpty(item.getHeadImagePath())) {
            Picasso.with(context).load(item.getHeadImagePath())
                    .placeholder(R.drawable.home_bg)
                    .into(headImage);
        }

        headImage.setClickable(false);
        headImage.setFocusable(false);
        headImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mMemberListCallBack != null){
                    mMemberListCallBack.onMemberClick(item);
                }
            }
        });
//        TextView textview =(TextView) convertView.findViewById(R.id.member_name);
//        textview.setText(""+item.getUserName());
        return convertView;
    }

    public void refreshMemberData(List<MemberInfo> list){
        this.list=list;

    }

    private MemberListCallBack mMemberListCallBack;
    public static interface MemberListCallBack{
        public void onMemberClick(MemberInfo memberInfo);
    }

}
